package gokyesil.game.aygitciklar.view;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.lights.Lights;
import com.badlogic.gdx.math.Matrix4;

import gokyesil.game.aygitciklar.model.Aygit;
import gokyesil.game.aygitciklar.model.Box;
import gokyesil.game.aygitciklar.model.Earth;
import gokyesil.game.aygitciklar.model.EndBox;
import gokyesil.game.aygitciklar.model.WhiteBox;

public class Renderer 
{
	private SpriteBatch spriteBatch;
	private Aygit aygit;
	private ArrayList<Earth> groundList;
	private TextureRegion sky;
	private TextureRegion ground;
	private TextureRegion whiteBox;
	private TextureRegion greenEnd;
	private TextureRegion gameOver;
	private LinkedList<Box> AIList;
	private AtlasRegion aygitOne;
	private EndBox eBox;
	private ParticleEffect p;
	
	public Renderer(Aygit _aygit, ArrayList<Earth> _groundList, LinkedList<Box> _AIList, EndBox _eBox)
	{
		aygit = _aygit;
		groundList = _groundList;
		AIList = _AIList;
		eBox = _eBox;
		spriteBatch = new SpriteBatch();
		p = new ParticleEffect();
		loadTextures();
	}
	private void loadTextures()
	{
		TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("images/textures.pack"));
		aygitOne = atlas.findRegion("aygit");
		//ground.loadTexture("earth", atlas);
		sky = atlas.findRegion("sky");
		ground = atlas.findRegion("earth");
		whiteBox = atlas.findRegion("wBox");
		greenEnd = atlas.findRegion("end");
		gameOver = atlas.findRegion("gameOver");
		p.load(Gdx.files.internal("assets/fire.p"), Gdx.files.internal("images"));
		p.setPosition(23.3f, 4.5f);
		
		
		
	}
	public void render(OrthographicCamera _camera, boolean canBeFinished, boolean isFinished)
	{
		p.update(0.01f);
		spriteBatch.begin();
		spriteBatch.setProjectionMatrix(_camera.combined);
		spriteBatch.draw(sky, -24, -15, 72, 45);
		
		if(isFinished)
		{
			spriteBatch.draw(gameOver, 5, 5, 14, 6f);
		}
		else
		{	
			aygit.render(spriteBatch, aygitOne);
			Iterator iter = groundList.iterator();
			while(iter.hasNext())
			{
				((Earth) iter.next()).render(spriteBatch, ground);
			}
			Iterator iter2 = AIList.iterator();
			while(iter2.hasNext())
			{
				((Box) iter2.next()).render(spriteBatch, whiteBox);
			}
			if(canBeFinished)
			{
				
				if(p.isComplete())
				{
					p.start();
				}
				//eBox.render(spriteBatch, greenEnd);
				p.draw(spriteBatch);
			}
		}
		spriteBatch.end();
		
	}
	public void render(OrthographicCamera _camera, boolean canBeFinished, boolean isFinished, PerspectiveCamera cam, ModelBatch modelBatch, ModelInstance ins, Lights lights) 
	{
		p.update(0.01f);
		spriteBatch.begin();
		modelBatch.begin(cam);
		spriteBatch.setProjectionMatrix(_camera.combined);
		spriteBatch.draw(sky, -24, -15, 72, 45);
		
		if(isFinished)
		{
			spriteBatch.draw(gameOver, 5, 5, 14, 6f);
		}
		else
		{	
			aygit.render(spriteBatch, aygitOne, modelBatch, ins, lights);
			Iterator iter = groundList.iterator();
			while(iter.hasNext())
			{
				((Earth) iter.next()).render(spriteBatch, ground);
			}
			Iterator iter2 = AIList.iterator();
			while(iter2.hasNext())
			{
				((Box) iter2.next()).render(spriteBatch, whiteBox);
			}
			if(canBeFinished)
			{
				
				if(p.isComplete())
				{
					p.start();
				}
				//eBox.render(spriteBatch, greenEnd);
				p.draw(spriteBatch);
			}
		}
		spriteBatch.end();
		modelBatch.end();
		float x = 0;
		float y = 0;
		if(Math.abs((aygit.body.getPosition().x-_camera.position.x)) > 5f)
		{
			x = (float) Math.pow((aygit.body.getPosition().x-_camera.position.x)/8.7f,5);
		}
		if(Math.abs((aygit.body.getPosition().y-_camera.position.y)) > 3f)
		{
			y = (float) Math.pow((aygit.body.getPosition().y-_camera.position.y)/8.7f,5);
		}
		_camera.position.add(x, y, 0);
		ins.transform.translate(-x, -y, 0);
		//_camera.position.set(aygit.body.getPosition().x,aygit.body.getPosition().y,0);
		//cam.position.set(aygit.body.getPosition().x,aygit.body.getPosition().y,0);
		//System.out.println(_camera.position);
		
	}
}
